page.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { notFound } from 'next/navigation';
  2. import { allPosts } from 'contentlayer/generated';
  3. import Mdx from '@/components/MDX';
  4. import Ad from '@/components/Ad';
  5. import Link from '@/components/Link';
  6. import Icon from '@/components/Icon';
  7. import { blogEnabled } from '@/config/site';
  8. interface PageProps {
  9. params: {
  10. slug: string;
  11. };
  12. }
  13. async function getPageFromParams(params) {
  14. const slug = params?.slug,
  15. page = allPosts.find((page) => page.slugAsParams === slug);
  16. if (!page) {
  17. null;
  18. }
  19. return page;
  20. }
  21. export async function generateStaticParams(): Promise<PageProps['params'][]> {
  22. return allPosts.map((page) => ({
  23. slug: page.slugAsParams,
  24. }));
  25. }
  26. export default async function PostPage({ params }: PageProps) {
  27. const post = await getPageFromParams(params);
  28. if (!post || !blogEnabled) {
  29. notFound();
  30. }
  31. return (
  32. <>
  33. <section className="section pt-0">
  34. <div className="container">
  35. <div>
  36. {post && post.title && (
  37. <div className="py-7 text-center">
  38. {post.product && <div className="hero-subheader">{post.product}</div>}
  39. <div className="text-muted mb-2">July 25, 2023</div>
  40. <h1 className="hero-title">{post.title}</h1>
  41. </div>
  42. )}
  43. </div>
  44. <div className="row">
  45. <div className="col">
  46. <div className="sticky-top">
  47. <Link href="/blog" className="link-muted">
  48. <Icon name="chevron-left" />
  49. Go back
  50. </Link>
  51. </div>
  52. </div>
  53. <div className="col-slim">
  54. <div className="markdown">
  55. {post.description && <p className="lead">{post.description}</p>}
  56. <Mdx code={post.body.code} />
  57. </div>
  58. </div>
  59. <div className="col">
  60. <div className="sticky-top">
  61. <div className="row justify-end">
  62. <div className="col-side">
  63. <Ad />
  64. </div>
  65. </div>
  66. </div>
  67. </div>
  68. </div>
  69. </div>
  70. </section>
  71. </>
  72. );
  73. }